package org.okkam.refine.strategy;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.client.data.AttributeMetadataType;
import org.okkam.client.data.AttributeType;
import org.okkam.client.data.AttributesType;
import org.okkam.client.data.Entity;
import org.okkam.client.data.ProfileType;
import org.okkam.client.util.XMLEntityConverter;
import org.okkam.refine.strategy.CandidateStrategy;
import org.okkam.refine.strategy.LocationCandidate;

public class LocationCandidate implements CandidateStrategy {
	
	private static Log log = LogFactory.getLog(LocationCandidate.class);

	@Override
        public boolean checkEquality(List<AttributeType> attributeTypeList, List<String> entityList) {
                   
	           Iterator<AttributeType> attributeTypeIterator = attributeTypeList.iterator();
	            Map<String, String> propValueNewEntity = new HashMap<String, String>();
	            boolean match = false;
	            
	            while (attributeTypeIterator.hasNext()) {
	                AttributeType attributeType = attributeTypeIterator.next();
	                String term = attributeType.getName().getLocalPart().toLowerCase();
	                String value = attributeType.getValue().toLowerCase().replace("\"", "");
	                AttributeMetadataType metadata = attributeType.getMetadata();
	                propValueNewEntity.put(term, value);
	        }            
	            
	            Iterator entityIterator = entityList.iterator();
	            while (entityIterator.hasNext()) {
	                    XMLEntityConverter converter = new XMLEntityConverter();
	                    Entity entity = null;
	                    try {
	                        entity = converter.xmlToEntity( entityIterator.next().toString());
	                    } catch (JAXBException e) {
	                        // TODO Auto-generated catch block
	                        e.printStackTrace();
	                    }
	                    ProfileType profile = entity.getProfile();
	                    AttributesType attributesType = profile.getAttributes();
	                    List<AttributeType> attributeTypeListResponse = attributesType.getAttributes();
	                    Iterator<AttributeType> attributeTypeIteratorresponse = attributeTypeListResponse.iterator();

	                    Map<String, String> propValueResponse = new HashMap<String, String>();

	                    while (attributeTypeIteratorresponse.hasNext()) {
	                            AttributeType attributeType = attributeTypeIteratorresponse.next();
	                            String term = attributeType.getName().getLocalPart().toLowerCase();
	                            String value = attributeType.getValue().toLowerCase().replace("\"", "");
	                            propValueResponse.put(term, value);
	                    }
	                        
	         //   Iterator it = propValueNewEntity.keySet().iterator();
          
            //based on istat code code and city name (CAP)
            if (propValueResponse.containsKey("istat_code")&&propValueNewEntity.containsKey("istat_code")
                    && propValueResponse.containsKey("city")&&propValueNewEntity.containsKey("city")) {

               // while (it.hasNext()) {
                //    String valOfQuery = propValueNewEntity.get(it.next()).replace("\"", "");
                    if ( propValueNewEntity.get("istat_code").equals(propValueResponse.get("istat_code").toLowerCase())
                            && propValueNewEntity.get("city").equals(propValueResponse.get("city").toLowerCase())) {
                        match = true;
                        return match;
                    }
              //  }

            }
         //based on city name, country,  ...            
            if ((propValueResponse.containsKey("city") 
                    && ((propValueResponse.containsKey("country")) 
                            || propValueResponse.containsKey("istat_code")))
           //Based on region name,...
                    || (propValueResponse.containsKey("region") 
                            && propValueResponse.containsKey("country"))
          //Based on province...
                    || (propValueResponse.containsKey("province") 
                            && propValueResponse.containsKey("country"))) {

                Map <String, String> ResponseValues = new HashMap<String, String>();
                if(propValueResponse.containsKey("city"))
                    ResponseValues.put("city",propValueResponse.get("city"));
                
                if(propValueResponse.containsKey("region"))
                    ResponseValues.put("region",propValueResponse.get("region"));
                
                if(propValueResponse.containsKey("province"))
                    ResponseValues.put("province",propValueResponse.get("province"));
                    
                if(propValueResponse.containsKey("postal_code"))
                    ResponseValues.put("postal_code",propValueResponse.get("postal_code"));
                
                if(propValueResponse.containsKey("country"))
                    ResponseValues.put("country",propValueResponse.get("country"));
                

                Iterator itNameCheck = ResponseValues.keySet().iterator();

                while (itNameCheck.hasNext()) {
                    match=false;
                    String responseProperty = (String) itNameCheck.next();
                    String responseValue = (String) ResponseValues.get(responseProperty);
                    
                    Iterator itq = propValueNewEntity.keySet().iterator();

                    while (itq.hasNext()) {
                        String newProperty = itq.next().toString().replace("\"", "");
                        String newValue = propValueNewEntity.get(newProperty).replace("\"", "");
                        if (newValue.toLowerCase().equals(responseValue.toLowerCase())) {
                            match = true;
                            break;
                        }
                    }
                    if (!match) {
                        break;
                    }
                }
                if(match){
                    return match;
                }
            }
        }
            return match;
        }
}